package com.jack.topvision.tvcamera2;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;

import com.topvision.jack.uploadlibrary.admin.TvUploadSdk;
import com.topvision.jack.uploadlibrary.task.UploadTask;
import com.topvision.jack.uploadlibrary.task.UploadTaskListener;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by gushenjie on 17/12/24.
 */
public class UploadActivity extends Activity implements View.OnClickListener, UploadTaskListener {
    //该地址为基础地址,后面的接口地址SDK会自动加上,目前是ckeckFileServlet和uploaderWithContinuinglyTransferring两个接口方法,请按规定定义后台接口方法
    public static final String BASE_API_URL = "http://topvision-cv.com/topvision-api/app/launcher/video/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload);
        initView();
        TvUploadSdk.getInstance(getApplicationContext()).registerNetWorkListener(BASE_API_URL, this);
    }

    private void initView() {
        findViewById(R.id.uploadPhoto).setOnClickListener(this);
        findViewById(R.id.uploadVideo).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.uploadVideo:
                //单次上传5M大小
                int blockLength = 1 * 1024 * 1024;
                String videoPath = Environment.getExternalStorageDirectory().getPath() + File.separator + "cameraTest/video/a.mp4";
//                TvUploadSdk.getInstance(getApplicationContext()).upload(BASE_API_URL, videoPath, blockLength, this);
                //不指定大小,默认为1M
                //TvUploadSdk.getInstance(getApplicationContext()).upload(VIDEO_URL, videoPath, this);
                Map<String,String> data = new HashMap<>();
                data.put("jack","123");
                TvUploadSdk.getInstance(getApplicationContext()).upload(BASE_API_URL, videoPath, blockLength, this,data);
                break;
            case R.id.uploadPhoto:
                int blockLength2 = 5 * 1024 * 1024;
                String photoPath = Environment.getExternalStorageDirectory().getPath() + File.separator + "topvision/a.jpg";

                TvUploadSdk.getInstance(getApplicationContext()).upload(BASE_API_URL, photoPath, blockLength2, this);
                break;
        }
    }

    @Override
    public void onUploading(UploadTask uploadTask, String percent, int position, String taskId) {
        if (uploadTask.getId().equals(taskId)) {
            Log.e("tvLog", "正在上传文件" + uploadTask.getFileName() + "," + percent + " % ");
        }
    }

    @Override
    public void onUploadSuccess(UploadTask uploadTask, File file, String taskId) {
        Log.e("tvLog", "上传完成 path：" + file.getAbsolutePath());
    }

    @Override
    public void onError(UploadTask uploadTask, int errorCode, int position, String taskId) {
        Log.e("tvLog", "上传失败,errorCode=" + errorCode + "taskId=" + taskId);
    }


    //暂停了某个任务回调
    @Override
    public void onPause(UploadTask uploadTask, String taskId) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        TvUploadSdk.getInstance(getApplicationContext()).unRegisterNetWorkListener();
    }
}
